package com.pms.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.pms.common.enums.RoomStatus;
import com.pms.common.utils.DateUtils;
import com.pms.common.utils.bean.BeanUtils;
import com.pms.common.utils.uuid.UUID;
import com.pms.system.domain.PmsClientInfo;
import com.pms.system.domain.PmsGuestroomInfo;
import com.pms.system.domain.PmsGuestroomItem;
import com.pms.system.domain.vo.PmsCheckinInfoVo;
import com.pms.system.mapper.PmsCheckinInfoMapper;
import com.pms.system.mapper.PmsClientInfoMapper;
import com.pms.system.mapper.PmsGuestroomInfoMapper;
import com.pms.system.mapper.PmsGuestroomItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pms.system.domain.PmsCheckinInfo;
import com.pms.system.service.IPmsCheckinInfoService;

/**
 * 入住信息Service业务层处理
 *
 * @author 达瓦里氏
 * @date 2023-03-04
 */
@Service
public class PmsCheckinInfoServiceImpl implements IPmsCheckinInfoService {
    @Autowired
    private PmsCheckinInfoMapper pmsCheckinInfoMapper;
    @Autowired
    private PmsGuestroomInfoMapper pmsGuestroomInfoMapper;
    @Autowired
    private PmsGuestroomItemMapper pmsGuestroomItemMapper;
    @Autowired
    private PmsClientInfoMapper pmsClientInfoMapper;

    /**
     * 查询入住信息
     *
     * @param id 入住信息主键
     * @return 入住信息
     */
    @Override
    public PmsCheckinInfoVo selectPmsCheckinInfoById(Long id) {
        return pmsCheckinInfoMapper.selectPmsCheckinInfoById(id);
    }

    /**
     * 查询入住信息列表
     *
     * @param pmsCheckinInfo 入住信息
     * @return 入住信息
     */
    @Override
    public List<PmsCheckinInfoVo> selectPmsCheckinInfoList(PmsCheckinInfo pmsCheckinInfo) {
        return pmsCheckinInfoMapper.selectPmsCheckinInfoList(pmsCheckinInfo);
    }

    /**
     * 新增入住信息
     *
     * @param pmsCheckinInfo 入住信息
     * @return 结果
     */
    @Override
    public int insertPmsCheckinInfo(PmsCheckinInfo pmsCheckinInfo) throws Exception {
        pmsCheckinInfo.setCreateTime(DateUtils.getNowDate());
        pmsCheckinInfo.setCode(UUID.randomCode());
        if (pmsCheckinInfo.getRoomId() != null) {
            // 新增入住信息将房间状态修改为已预订
            PmsGuestroomInfo pmsGuestroomInfo = pmsGuestroomInfoMapper.selectPmsGuestroomInfoById(pmsCheckinInfo.getRoomId());
            pmsGuestroomInfo.setType(RoomStatus.RESERVED.getCode());
            pmsGuestroomInfoMapper.updatePmsGuestroomInfo(pmsGuestroomInfo);
            // 计算入住天数
            BigDecimal days = BigDecimal.valueOf(DateUtils.differentDaysByMillisecond(pmsCheckinInfo.getCheckinTime(), pmsCheckinInfo.getCheckoutTime()));
            // 计算价格 房间价格*天数
            PmsGuestroomItem pmsGuestroomItem = pmsGuestroomItemMapper.selectPmsGuestroomItemById(pmsGuestroomInfo.getItemId());
            BigDecimal price = pmsGuestroomItem.getPrice().multiply(days);
            pmsCheckinInfo.setPrice(price);
            return pmsCheckinInfoMapper.insertPmsCheckinInfo(pmsCheckinInfo);
        }
        throw new RuntimeException("预定房间号为空");
    }


    /**
    * className: PmsCheckinInfoServiceImpl
    * @Description: 立即预定
    * @Author:
    * @Date: 2023/4/3
    * @Time: 15:43
    */
    @Override
   public int insertPmsCheckinByUser(PmsCheckinInfoVo pmsCheckinInfoVo){

        // 新增客户

        // 新增入住信息


       pmsCheckinInfoVo.setCode(UUID.randomCode());
       if (pmsCheckinInfoVo.getRoomId() != null) {
           // 新增入住信息将房间状态修改为已预订
           PmsGuestroomInfo pmsGuestroomInfo = pmsGuestroomInfoMapper.selectPmsGuestroomInfoById(pmsCheckinInfoVo.getRoomId());
           pmsGuestroomInfo.setType(RoomStatus.RESERVED.getCode());
           pmsGuestroomInfoMapper.updatePmsGuestroomInfo(pmsGuestroomInfo);
           // 计算价格 房间价格*天数
           PmsGuestroomItem pmsGuestroomItem = pmsGuestroomItemMapper.selectPmsGuestroomItemById(pmsGuestroomInfo.getItemId());
           BigDecimal price = pmsGuestroomItem.getPrice().multiply(BigDecimal.valueOf(pmsCheckinInfoVo.getCheckinDay()));
           pmsCheckinInfoVo.setPrice(price);

           return pmsCheckinInfoMapper.insertPmsCheckinInfoVo(pmsCheckinInfoVo);

       }
       throw new RuntimeException("预定房间号为空");


   }

    /**
     * 修改入住信息
     *
     * @param pmsCheckinInfo 入住信息
     * @return 结果
     */
    @Override
    public int updatePmsCheckinInfo(PmsCheckinInfo pmsCheckinInfo) {
        pmsCheckinInfo.setUpdateTime(DateUtils.getNowDate());
        if (pmsCheckinInfo.getRoomId() != null) {
            // 新增入住信息将房间状态修改为已预订
            PmsGuestroomInfo pmsGuestroomInfo = pmsGuestroomInfoMapper.selectPmsGuestroomInfoById(pmsCheckinInfo.getRoomId());
            pmsGuestroomInfo.setType(RoomStatus.RESERVED.getCode());
            pmsGuestroomInfoMapper.updatePmsGuestroomInfo(pmsGuestroomInfo);
            // 计算入住天数
            BigDecimal days = BigDecimal.valueOf(DateUtils.differentDaysByMillisecond(pmsCheckinInfo.getCheckinTime(), pmsCheckinInfo.getCheckoutTime()));
            // 计算价格 房间价格*天数
            PmsGuestroomItem pmsGuestroomItem = pmsGuestroomItemMapper.selectPmsGuestroomItemById(pmsGuestroomInfo.getItemId());
            BigDecimal price = pmsGuestroomItem.getPrice().multiply(days);
            pmsCheckinInfo.setPrice(price);
        }
        return pmsCheckinInfoMapper.updatePmsCheckinInfo(pmsCheckinInfo);
    }

    /**
     * 批量删除入住信息
     *
     * @param ids 需要删除的入住信息主键
     * @return 结果
     */
    @Override
    public int deletePmsCheckinInfoByIds(Long[] ids) {
        return pmsCheckinInfoMapper.deletePmsCheckinInfoByIds(ids);
    }

    /**
     * 删除入住信息信息
     *
     * @param id 入住信息主键
     * @return 结果
     */
    @Override
    public int deletePmsCheckinInfoById(Long id) {
        return pmsCheckinInfoMapper.deletePmsCheckinInfoById(id);
    }


    /**
     * 办理入住
     *
     * @Author: 达瓦里氏
     */
    @Override
    public int checkInByUserCode(Long id) {
        // 通过身份证号查询是否有入住信息
        PmsCheckinInfo pmsCheckinInfo = new PmsCheckinInfo();
        pmsCheckinInfo.setClientId(id);
        List<PmsCheckinInfoVo> pmsCheckinInfoVoList = pmsCheckinInfoMapper.selectPmsCheckinInfoList(pmsCheckinInfo);
        if (pmsCheckinInfoVoList.size() > 0) {
            PmsGuestroomInfo pmsGuestroomInfo = pmsGuestroomInfoMapper.selectPmsGuestroomInfoById(pmsCheckinInfoVoList.get(0).getRoomId());
            pmsGuestroomInfo.setType(RoomStatus.CHECKEDIN.getCode());
            return pmsGuestroomInfoMapper.updatePmsGuestroomInfo(pmsGuestroomInfo);
        } else {
            throw new RuntimeException("未查询到当前客户预约信息");
        }
    }


    /**
     * 退房
     *
     * @param id 入住信息id
     * @return 结果
     */
    @Override
    public int CheckOut(Long id) {
        PmsCheckinInfoVo pmsCheckinInfoVo = pmsCheckinInfoMapper.selectPmsCheckinInfoById(id);
        pmsCheckinInfoVo.setUpdateTime(DateUtils.getNowDate());
        PmsGuestroomInfo pmsGuestroomInfo = pmsGuestroomInfoMapper.selectPmsGuestroomInfoById(pmsCheckinInfoVo.getRoomId());
        // 判断客房状态是否为已预订
        if (pmsCheckinInfoVo.getRoomType().equals(RoomStatus.RESERVED.getCode())) {
            // 入住信息将房间状态修改为空闲
            pmsGuestroomInfo.setType(RoomStatus.LEISURE.getCode());
        } else {
            // 入住信息将房间状态修改为已退房
            pmsGuestroomInfo.setType(RoomStatus.CHECKEDOUT.getCode());
        }
        pmsGuestroomInfoMapper.updatePmsGuestroomInfo(pmsGuestroomInfo);
        PmsCheckinInfo pmsCheckinInfo = new PmsCheckinInfo();
        BeanUtils.copyProperties(pmsCheckinInfoVo, pmsCheckinInfo);
        return pmsCheckinInfoMapper.updatePmsCheckinInfo(pmsCheckinInfo);
    }
}
